//登陆函数 
function login() {
	var url = "loginAction";
	var username = $.trim($("#username").val()); //get user's name
	var password = $.trim($("#password").val()); //get user's password
	var json_user = {
		username : username,
		password : password
	};

	if (username == "") {
		document.getElementById("error").style.display = "block";
		document.getElementById("error").innerHTML = "Please enter your username";
	} else if (password == "") {
		document.getElementById("error").style.display = "block";
		document.getElementById("error").innerHTML = "Please enter your password";
	} else {
		$.post(url, json_user, callback, "json");

		function callback(json) {
			var json_object = eval("(" + json + ")");
			var suc = json_object.suc;
			if (suc == 0) {
				alert("登陆失败啦!!!");
			} else {
				window.location.href = "../viewBookListAction";
			}
		}
	}
}

//注册函数 
function regist() {
	var url = "registAction";
	var name = $.trim($("#realname").val()); //get user's name
	var username = $.trim($("#username").val()); //get user's name
	var password = $.trim($("#password").val()); //get user's password
	var confirm_password = $.trim($("#confirm_password").val()); //get user's password
	var email = $.trim($("#email").val()); //get user's email
	var json_user = {
		name : name,
		username : username,
		password : password,
		email : email
	};

	if (name == "") {
		document.getElementById("error").style.display = "block";
		document.getElementById("error").innerHTML = "Please enter your realname";
	} else if (username == "") {
		document.getElementById("error").style.display = "block";
		document.getElementById("error").innerHTML = "Please enter your username";
	} else if (email == "") {
		document.getElementById("error").style.display = "block";
		document.getElementById("error").innerHTML = "Please enter your email";
	} else if (!checkEmail(email)) {
		document.getElementById("error").style.display = "block";
		document.getElementById("error").innerHTML = "email地址不符合";
	} else if (password == "") {
		document.getElementById("error").style.display = "block";
		document.getElementById("error").innerHTML = "Please enter your password";
	} else if (password != confirm_password) {
		document.getElementById("error").style.display = "block";
		document.getElementById("error").innerHTML = "two password must be the same.";
	} else {
		$.post(url, json_user, callback, "json");

		function callback(json) {
			alert(json);
		}
	}
}

function checkEmail(email) {
	var pattern = /^([a-zA-Z0-9_-])+@([a-zA-Z0-9_-])+(.[a-zA-Z0-9_-])+/;
	return pattern.test(email);
}
function deleteUser(id) {
	var url = "deleteUserAction";
	var json_book = {
		id : id
	};

	if (id == "") {
		return;
	} else {
		$.post(url, json_book, callback, "json");
		function callback(json) {
			var json_object = eval("(" + json + ")");
			var suc = json_object.suc;
			if (suc == 0) {
				document.getElementById("bookDetailContent").innerHTML = "<div style='margin:20px;'>"
						+ "<p><span style='font-weight:bold;'>"
						+ json_object.msg + "</span></p>" + "</div>";
				$('#viewBookDetail').skygqbox( {
					title : "错误",
					shut : "关闭",
					opacity : 0.2
				});
			} else {
				document.getElementById("bookDetailContent").innerHTML = "<div style='margin:20px;'>"
						+ "<p><span style='font-weight:bold;'>删除成功</span></p>"
						+ "</div>";
				$('#viewBookDetail').skygqbox( {
					title : "成功",
					shut : "关闭",
					opacity : 0.2
				});
				window.location.href = "viewUserListAction";
			}
		}
	}
}

//罚金清零
function resetFine(userId) {
	var url = "resetFineAction";
	var json_user = {
		id : userId
	};
	$.post(url, json_user, callback, "json");
	function callback(json) {
		window.location.href = "viewUserListAction";
	}
}
//
//恢复状态
function resetType(userId) {
	var url = "resetTypeAction";
	var json_user = {
		id : userId
	};
	$.post(url, json_user, callback, "json");
	function callback(json) {
		window.location.href = "viewUserListAction";
	}
}
